package cn.jn.lhm.cms.service.impl;

import cn.jn.lhm.cms.entity.CrmBanner;
import cn.jn.lhm.cms.mapper.CrmMapper;
import cn.jn.lhm.cms.service.CrmService;
import cn.jn.lhm.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author LHM
 * @Tips 热爱每一天，奋斗每一天
 * @date 2020/6/9 - 19:15
 */
@Service
public class CrmServiceImpl implements CrmService {

    @Resource
    private CrmMapper crmMapper;


    @Override
    public R page(Page<CrmBanner> bannerPage) {
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_modified");
        IPage<CrmBanner> iPage = crmMapper.selectPage(bannerPage, wrapper);
        return R.ok().data("data", iPage.getRecords()).data("total", iPage.getTotal());
    }

    @Override
    public void save(CrmBanner crmBanner) {
        crmMapper.insert(crmBanner);
    }

    @Override
    public void updateBannerInfo(CrmBanner crmBanner) {
        crmMapper.updateById(crmBanner);
    }

    @Override
    public void deleteBannerInfo(String id) {
        crmMapper.deleteById(id);
    }


    @Cacheable(value = "banner")
    @Override
    public List<CrmBanner> selectAllBannerInfo() {
        //按照sort进行降序排序，限制查询前两条
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("sort");
        wrapper.last("limit 2");

        List<CrmBanner> list = crmMapper.selectList(wrapper);
        return list;
    }
}
